# pymq - The python based Message Queue
#
# Copyright(C) 2010, Dhruv Matani(dhuvbird@gmail.com)
#
# pymq is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# pymq is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with pymq. If not, see
# <http://www.gnu.org/licenses/>.
#


from handler_ah import handler_ah
from handler_queue import handler_queue
import queue_globals as vars


class request_dispatcher:
    def __init__(self):
        vars.handler_ah = handler_ah()
        vars.handler_queue = handler_queue()
        self.handlers = { "/queue/": vars.handler_queue,
                         "/_ah/": vars.handler_ah,
        }

    # The handler_ah will call these functions.
    # These functions will in turn call the corresponding functions in the
    # Queue Manager that is actually managing this queue.
    #
    # NOTE: This function is so named since the handler_queue object should NOT
    # call this function since it runs in the same thread.
    def informQueueHandler(self, request):
        vars.handler_queue.informQueueHandler(request)

    def queueRequest(self, request):
        requestHandled = False
        for h in self.handlers:
            if request.resource.find(h) == 0:
                self.handlers[h].handleRequest(request)
                requestHandled = True
                break

        if not requestHandled:
            request.error(404)
